package com.gsnd.briuppts.web.controller;

import com.gsnd.briuppts.entity.BaseAuthority;
import com.gsnd.briuppts.entity.DO.BaseAuthorityExtend;
import com.gsnd.briuppts.entity.DO.BaseAuthoritySimple;
import com.gsnd.briuppts.result.Response;
import com.gsnd.briuppts.service.BaseAuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class BaseAuthorityController {
    @Autowired
    BaseAuthorityService authorityService;

    @GetMapping("/current/authorities")
    public Response<List<BaseAuthoritySimple>> currentAuthorities(HttpSession session){
        // 获取用户id
        Object obj = session.getAttribute("userId");
        // 实际中需要写一个断言进行验证
        assert obj instanceof String; // 小拓展
        String userId = (String) obj;
        List<BaseAuthoritySimple> authorities = authorityService.findAllAuthorities(userId);
        return Response.success(authorities);
    }

    @GetMapping("/current/authorityTree")
    public Response<List<BaseAuthorityExtend>> currentAuthorityTree(HttpSession session){
        // 获取用户id
        Object obj = session.getAttribute("userId");
        // 实际中需要写一个断言进行验证
        assert obj instanceof String; // 小拓展
        String userId = (String) obj;
        List<BaseAuthorityExtend> authorities = authorityService.findAllAuthoritiesTree(userId);
        return Response.success(authorities);
    }
}
